package com.usth.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.usth.model.Airport;
import com.usth.model.FlightCom;
import com.usth.model.FlightInfo;
import com.usth.model.FlightTranVO;
import com.usth.model.TransitPrice;

public class FlightDaoImpl extends HibernateDaoSupport implements IFlightDao {

	public List<Airport> listair() {
		return this.getHibernateTemplate().find("from Airport");
	}

	public List<FlightCom> listcom() {
		return this.getHibernateTemplate().find("from FlightCom");
	}

	public void addflight(FlightInfo fi) {
		this.getHibernateTemplate().save(fi);
	}

	public List<FlightInfo> listflights() {
		return this.getHibernateTemplate().find("from FlightInfo");
	}

	public void flightedit(FlightInfo fi) {
		FlightInfo f = (FlightInfo) this.getHibernateTemplate().get(
				"com.usth.model.FlightInfo", fi.getFlightId());
		fi.setTransitAirport(f.getTransitAirport());
		this.getHibernateTemplate().update(fi);

	}

	public void delflight(String fid) {
		FlightInfo fi = (FlightInfo) this.getHibernateTemplate().get(
				"com.usth.model.FlightInfo", fid);
		this.getHibernateTemplate().delete(fi);
	}

	public List<FlightTranVO> pretran() {
		List<FlightInfo> li=this.listflights();
		List<FlightTranVO> ftplist=new ArrayList<FlightTranVO>();
		for (FlightInfo flightInfo : li) {
			String trannames="";
			FlightTranVO tvo=new FlightTranVO();
			tvo.setFlightId(flightInfo.getFlightId());
			if(flightInfo.getTransitAirport()!=null&&flightInfo.getTransitAirport().length()!=0){
				String[] tranid=flightInfo.getTransitAirport().split(",");
				for (String s : tranid) {
					Airport tp=(Airport) this.getHibernateTemplate().get("com.usth.model.Airport", Integer.parseInt(s));
					trannames+=tp.getAirportName()+",";
				}
				tvo.setAirportName(trannames.trim().substring(0,trannames.trim().length()-1));
			}
			ftplist.add(tvo);
		}
		return ftplist;
	}

}
